from django.db import models
from django.contrib.auth.models import User
from mdeditor.fields import MDTextField
import markdown

# Create your models here.

GENDER = (("male", "男"), ("female", "女"), ("null", "保密"))
USERTYPE = {
    ("01-student", "学员"),
    ("02-teacher", "任课教师"),
    ("03-admin", "教务老师"),
    ("10-user", "普通用户"),
}


class Subject(models.Model):
    """
    课程
    """
    name=models.CharField(max_length=1024, verbose_name="已购课包")
    class Meta:
        verbose_name = "已购课包"
        verbose_name_plural = "已购课包"  # 侧边栏

    def __str__(self):
        return str(self.name)


class MyUser(models.Model):
    """扩展自带User"""

    user = models.OneToOneField(
        User, on_delete=models.CASCADE, verbose_name="用户名")
    real_name = models.CharField(
        max_length=1000, default="", verbose_name="真实姓名", blank=True,
    )
    score = models.FloatField(verbose_name="得分", default=0, blank=True)
    age = models.IntegerField(verbose_name="年龄", default=3, blank=True)
    gender = models.CharField(
        max_length=32,
        choices=GENDER,
        default="保密",
        verbose_name="性别",
        blank=True,
    )
    avatar = models.CharField(
        verbose_name="头像地址",
        max_length=1000,
        default="/static/home/imgs/face.png",
        blank=True,
    )
    signature = models.CharField(
        max_length=1000, default="", verbose_name="个性简介", blank=True
    )
    subjects=models.ManyToManyField(Subject, verbose_name="已购课包", blank=True)
    workload = models.FloatField(verbose_name="剩余课时", default=0, blank=True)
    money = models.FloatField(verbose_name="剩余费用", default=0, blank=True)
    user_type = models.CharField(
        max_length=20,
        choices=USERTYPE,
        default="普通用户",
        verbose_name="账户类型",
        blank=True,
    )
    teacher = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        verbose_name="服务教师",
        blank=True,
        null=True,
    )
    note = MDTextField(verbose_name="备注", blank=True, default="")

    class Meta:
        verbose_name = "二级账户"
        verbose_name_plural = "二级账户"  # 侧边栏

    def __str__(self):
        """获取用户名"""
        if self.real_name:
            return str(self.real_name)
        elif self.user.first_name and self.user.last_name:
            return str(self.user.last_name + self.user.first_name)
        else:
            return str(self.user.username)